package com.tronsis.distribution.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.MainActivity;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.db.DBUtil;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.IntentUtil;
import com.tronsis.library.util.KeyboardUtils;
import com.tronsis.library.util.SharedPreferencesUtil;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends ToolBarBaseActivity {

    @BindView(R.id.et_phone)
    EditText mEtPhone;

    @BindView(R.id.et_password)
    EditText mEtPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setTitle(false, getString(R.string.login));
        SharedPreferencesUtil.clear(mContext);
        DBUtil.getInstance(mContext).deleteAll(UserBean.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100&&resultCode==RESULT_OK){
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra("isRegister",true);
            startActivity(intent);
            finish();
        }
    }

    @OnClick(R.id.tv_forget_password)
    void forgetPasswordClick() {
        Intent intent = new Intent(this, ResetPasswordActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.tv_register)
    void registerClick() {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivityForResult(intent,100);
    }


    @OnClick(R.id.btn_login)
    void loginClick() {
        String phone = mEtPhone.getText().toString();
        String password = mEtPassword.getText().toString();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showMessage(mContext, "请输入手机号");
            return;
        }

        if (!StringUtil.isMobile(phone)) {
            ToastUtil.showMessage(mContext, "请输入正确的手机号");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.showMessage(mContext, "请输入密码");
            return;
        }

        KeyboardUtils.hideSoftInput(this);
        httpLogin(phone, password);
    }

    private void httpLogin(String phone, String password) {
        try {
            UserBizFactory.createBiz().login(this, phone, password, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    Intent intent = new Intent(mContext, MainActivity.class);
                    intent.putExtra("isRegister",false);
                    startActivity(intent);
                    IntentUtil.goBackToActivityRefresh(mContext,MainActivity.class);
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }


}
